package javaCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lxy_920912 on 03/05/2017.
 */
public class LeetCode500 {
    public String[] findWords(String[] words) {

        String[] str = {"qwertyuiop","asdfghjkl","zxcvbnm"};
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0;i < str.length;i++){
            for(int j = 0;j < str[i].length();j++){
                map.put(str[i].charAt(j),i);
            }
        }
        List<String> list = new ArrayList<>();
        for(int i = 0;i < words.length;i++){
            String temp = words[i].toLowerCase();
            int index = map.get(temp.charAt(0));
            boolean flag = true;

            for(int j = 1;j < temp.length();j++){
                if(map.get(temp.charAt(j)) != index){
                    flag = false;
                    break;
                }
            }
            if (flag){
                list.add(words[i]);
            }

        }
        return (String[])list.toArray(new String[0]);
    }
}
